package edu.hawaii.myisern.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.io.IOException;

import javax.xml.bind.JAXBException;

import org.junit.Test;

import edu.hawaii.myisern.collaborations.jaxb.Collaboration;
import edu.hawaii.myisern.organizations.jaxb.Organization;
import edu.hawaii.myisern.researchers.jaxb.Researcher;

/**
 * Tests the Id class.
 * 
 * @author Randy Cox
 */ 
// just for JUnit testing, literals are more readable
@SuppressWarnings("PMD.AvoidDuplicateLiterals") 
public class TestId {

  /**
   * Test buildId from a typical name field.
   */
  @Test
  public void testBuldId() {
    String name = "  One TWO Three Four five  ";
    String expected = "ONE_TWO_THREE_FOUR_FIVE";
    assertEquals("Id should replace spaces with underscore", expected, Id.build(name));
  }

  /**
   * Test creation of Id class.
   */
  @Test
  public void testIdConstructor() {
    String name = "  One TWO Three Four five  ";
    String expected = "ONE_TWO_THREE_FOUR_FIVE";

    Id id = new Id();
    assertEquals("Id should be empty", 0, id.toString().length());

    id = new Id(name);
    assertEquals("Id should be in ID form", expected, id.toString());
    assertEquals("Id should be in ID form", expected, id.getId());
    
    Id id1 = new Id("Test Name");
    Id id2 = new Id("Test Name");
    assertEquals("Hashcodes should be the same", id1.hashCode(), id2.hashCode());
  }

  /**
   * Test equals method.
   */
  @Test
  public void testEquals() {
    String name1 = "  One TWO Three Four five  ";
    String name2 = "  One TWO Three Four five  ";
    String name3 = "  One TWO Three Four five six  ";

    Id id1 = new Id(name1);
    Id id2 = new Id(name2);
    assertEquals("Ids are equal", id1, id2);

    Id id3 = new Id(name3);
    assertFalse("Ids are not equal", id1.equals(id3));
    id2.setId(name3);
    assertEquals("Ids are equal", id3, id2);
  }
  
  /**
   * Test getId methods.
   * The return Id should equal to that create from its name. 
   * @throws IOException on bad data
   * @throws JAXBException if a parser cannot be created
   */
  @Test
  public void testGetId() throws JAXBException, IOException {
    IsernModel model = new IsernModel();
    model.setPath(System.getProperty("user.dir") + "/xml/examples");
    model.setCollaborationsFile("collaborations.test.xml");
    model.setResearchersFile( "researchers.test.xml");
    model.setOrganizationsFile("organizations.test.xml");
    model.load();
    Collaboration collab = model.getCollaborationList().get(0);
    Id collabId = new Id(collab.getName());
    Organization org = model.getOrganizationList().get(0);
    Id orgId = new Id(org.getName());
    Researcher rsc = model.getResearcherList().get(0);
    Id rscId = new Id(rsc.getName());
    assertEquals("2 Collaboration Id should be the same.", collabId, Id.getId(collab));
    assertEquals("2 Organization Id should be the same.", orgId, Id.getId(org));
    assertEquals("2 Researcher Id should be the same.", rscId, Id.getId(rsc));
  }
}
